package com.example.innovate.controller;

import com.example.innovate.bean.Result;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.ResumeDO;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/resume")
@Api(tags = "简历信息接口")
public class ResumeController {
    @Autowired
    ResumeService resumeService;
    @Autowired
    AdminService adminService;

    @PostMapping("/addResume")
    @ApiOperation("添加简历信息")
    public Result addResume(@RequestBody ResumeDO resumeDO,
                            @RequestParam("userId") Integer userId) throws Exception{
        if(userId!=0){
            resumeDO.setReUserId(userId);
        }
        resumeService.addInfo(resumeDO);
        return new Result(200,"success","添加成功",resumeDO);
    }

    @DeleteMapping("/deleteResume")
    @ApiOperation("删除简历信息")
    public Result deleteResume(@RequestParam("id") int id,
                               @RequestParam("adminUserName") String adminUserName) throws Exception{
        ResumeDO resumeDO = resumeService.findById(id);
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(resumeDO!=null){
            if(adminInfoDO!=null){
                if(adminInfoDO.getAdminIsUpdate().equals(false)){
                    return new Result(400,"error","没有删除权限",resumeDO);
                }
            }
            resumeService.deleteInfo(id);
            return new Result(200,"success","删除成功",resumeDO);
        }
        else {
            return new Result(400,"error","信息不存在",resumeDO);
        }

    }

    @PostMapping("/updateResume")
    @ApiOperation("修改简历信息")
    public Result updateResume(@RequestBody ResumeDO resumeDO,
                               @RequestParam("adminUserName") String adminUserName) throws Exception{
        ResumeDO resumeDO1 = resumeService.findById(resumeDO.getReId());
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(resumeDO1!=null){
            if(adminInfoDO!=null){
                if(adminInfoDO.getAdminIsUpdate().equals(false)){
                    return new Result(400,"error","没有修改权限",resumeDO);
                }
            }
            resumeService.updateInfo(resumeDO);
            return new Result(200,"success","修改成功",resumeDO);
        }
        else {
            return new Result(400,"error","信息不存在",resumeDO);
        }
    }

    @GetMapping("/findResume")
    @ApiOperation("查找简历信息")
    public Result findAllResume(@RequestParam("pageNumber") Integer pageNumber,
                                     @RequestParam("pageSize") Integer pageSize,
                                     @RequestParam("userId") int userId) throws Exception{
        List<ResumeDO> list = resumeService.findInfo(pageNumber,pageSize,userId);
        return new Result(200,"success","查找成功",list);

    }
}
